<?php
namespace app\member\model;
use think\ Model;

/**
 * 
 */
class TpUser extends  Model
{
	
	//处理ajax登录信息(user.js userLogin)
	public function login($data){
		$userData=array();
		$userData['username']=trim($data['username']);
		$userData['password']=md5($data['password']);
		$users=$this->where(array('username'=>$userData['username']))
		->whereOr(array('mobile_phone'=>$userData['username']))
		->whereOr(array('email'=>$userData['username']))->find();
		if($users){
			if($users['password']==$userData['password']){
				
				//存在remember字段，写入cookie用于保存登录信息
				if(isset($data['remember'])){
					$aMonth=30*24*60*60;
					$username=encryption($users['username'],0);//加密cookie
					$password=encryption($data['password'],0);
					cookie('username', $username, $aMonth,'/');
					cookie('password', $password, $aMonth,'/');
				}
	

				//登录成功，用户id和用户名写入session
				session('uid',$users['id']);
				session('username',$users['username']);

				//登录成功，用户的会员等级和折扣率写入session
				$points=$users['points'];
				$memberLevel=db('tp_member_level')->where('bom_point','<=',$points)
				->where('top_point','>=',$points)->find();
				session('memberId',$memberLevel['id']);
				session('memberRate',$memberLevel['rate']);

				$arr=[
					'error'=>0,
					'message'=>"",
				];
				return json($arr);
			}else{
				$arr=['error'=>1, 'message'=> "用户或者密码错误", 'url'=>""];
				return json($arr);
			}
		}else{
			$arr=['error'=>1, 'message'=> "用户或者密码错误", 'url'=>""];
			return json($arr);
		}
	}


	public function logout(){
		session(null);
		cookie('username',null);
		cookie('password',null);


	}





}